// pages/audio/audio.js

var app = getApp();

var categories = new Array();
var multiArray = new Array();
var multiIndex = [0, 0];
var firstChoose = true;
var preMultiIndex = new Array();
var preMultiArray = new Array();

var curAudioPage = 1;
var curAudioCat = 0;
var curAudioSort = 1;

var audioSize = 20;




Page({

  /**
   * 页面的初始数据
   */
  data: {
    multiArray: multiArray,
    multiIndex: multiIndex,
    articles:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var _this = this;
    init(_this);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },
  /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
  onPullDownRefresh: function () {
    var _this = this;
    init(_this);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var _this = this;
    createAudios(_this, curAudioCat, curAudioSort, curAudioPage + 1, audioSize);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }, 
  initMultiArray: function () {
    var subMultiArray = new Array();
    subMultiArray.push("热门");

    for (var index in categories) {
      subMultiArray.push(categories[index].name);
    }
    multiArray = [subMultiArray, ['热门']];
    this.setData({ multiArray: multiArray });
  },
  bindMultiPickerChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value);
    this.setData({
      multiIndex: e.detail.value,
      audios: []
    });

    firstChoose = true;

    var parenName = this.data.multiArray[0][e.detail.value[0]];
    var childName = this.data.multiArray[1][e.detail.value[1]];

    curAudioCat = getCurCat(parenName, childName);
    curAudioSort = 2;
    curAudioPage = 1;
    if (curAudioCat == 0) {
      curAudioSort = 1;
    }

    createAudios(this, curAudioCat, curAudioSort, curAudioPage, audioSize);
  },
  bindMultiPickerColumnChange: function (e) {
    if (firstChoose) {
      firstChoose = false;
      preMultiArray = JSON.parse(JSON.stringify(this.data.multiArray));
      preMultiIndex = JSON.parse(JSON.stringify(this.data.multiIndex));
    }
    // console.log('修改的列为', e.detail.column, '，值为', e.detail.value);

    var data = {
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex
    };
    data.multiIndex[e.detail.column] = e.detail.value;

    var curColumn = e.detail.column;
    if (curColumn == 0) {
      var multiArrayChildren = getMultiArrayChildren(e.detail.value);
      data.multiArray[1] = multiArrayChildren;
    }

    this.setData(data);
  },
  bindMultiPickerCancel: function (e) {
    var data = {
      multiArray: preMultiArray,
      multiIndex: preMultiIndex
    };
    this.setData(data);

    firstChoose = true;
  }
});



function init(_this) {
  wx.request({
    url: app.globalData.requestUrl + '/audio/categories',
    data: '',
    header: {},
    method: 'POST',
    dataType: 'json',
    responseType: 'text',
    success: function (res) {
      if (res.data.responseCode === "000") {
        categories = res.data.data;
        _this.initMultiArray(categories);
      }

    },
    fail: function (res) { },
    complete: function (res) { },
  });

  //重置数据
  curAudioCat = 0;
  curAudioPage = 1;
  curAudioSort = 1;
  _this.setData({ "audios": [], multiArraynew : Array(), multiIndex :[0, 0] });

  createAudios(_this, curAudioCat, curAudioSort, curAudioPage, audioSize);
}


function createAudios(_this, cat, sort, page, size) {
  var reqData = {
    request:
      {
        cat: [cat], sort: sort, page: page, size: size
      }
  };

  wx.request({
    url: app.globalData.requestUrl + '/audio/list',
    data: JSON.stringify(reqData),
    header: {},
    method: 'POST',
    dataType: 'json',
    responseType: 'text',
    success: function (res) {
      var curAudios = new Array();
      if (res.data.responseCode === "000") {
        var items = res.data.data.items;
        curAudios = getCurAudios(items);

        curAudioPage = page;
        curAudioCat = cat;
        curAudioSort = sort;

        for (var _index in curAudios) {
          _this.data.audios.push(curAudios[_index]);
        }

        curAudios = _this.data.audios;

        _this.setData({ "audios": curAudios });
      }

    },
    fail: function (res) { },
    complete: function (res) { },
  });
}

function getCurAudios(items) {

  var curAudios = new Array();

  for (var index in items) {
    var item = items[index];

    var curAudio = { id: item.id, title: item.title, url: item.playTypes[0].url, coverUrl: item.coverUrl };

    curAudios.push(curAudio);
  }

  return curAudios;
}


function getMultiArrayChildren(curIndex) {
  if (curIndex == 0) {
    return new Array();
  }
  curIndex = curIndex - 1;

  var curChindren = categories[curIndex].childDCategories;

  var curMultiArrayChildren = new Array();

  for (var chindIndex in curChindren) {
    curMultiArrayChildren.push(curChindren[chindIndex].name);
  }

  return curMultiArrayChildren;
}


function getCurCat(parenName, childName) {
  if (parenName === "热门") {
    return 0;
  }

  for (var index in categories) {
    var curCategory = categories[index];
    if (curCategory.name === parenName) {
      return getCurCat4Child(curCategory.childDCategories, childName);
    }
  }
  return 0;
}



function getCurCat4Child(childDCategories, childName) {
  for (var index in childDCategories) {
    var curCategory = childDCategories[index];
    if (curCategory.name === childName) {
      return curCategory.id;
    }
  }

  return 0;
}